package com.huawei.esdk.uc.sametime.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Hashtable;

import org.apache.log4j.Logger;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;

/**
 * @author YUSI
 * 
 */
public class ImageFactory
{

    private static Logger logger = Logger.getLogger(ImageFactory.class);

    // 将构造方法设置为private，禁止创建该类的实例
    private ImageFactory()
    {
    }

    private static final String SEPARATOR = "/";

    // 图片保存的绝对地址
    private static final String REAL_PATH = SEPARATOR + "images" + SEPARATOR;

    // 图片名称的常量
    public static final String CALL_HOLD = "call_hold" + SEPARATOR
            + "CallHold_1.png";

    public static final String CALL_HOLD_DISABLED = "call_hold" + SEPARATOR
            + "CallHold_4.png";

    public static final String CALL_RESUME = "call_hold" + SEPARATOR
            + "Resume_1.png";

    public static final String CALL_RESUME_DISABLED = "call_hold" + SEPARATOR
            + "Resume_4.png";

    public static final String MUTE = "microphone" + SEPARATOR + "Mic_1.png";

    public static final String MUTE_DISABLED = "microphone" + SEPARATOR
            + "Mic_4.png";

    public static final String MUTED = "microphone" + SEPARATOR + "Mic_5.png";

    public static final String MUTED_DISABLED = "microphone" + SEPARATOR
            + "Mic_8.png";

    public static final String SPEAKER_ADJUST = "speaker" + SEPARATOR
            + "speaker_1.png";

    public static final String SPEAKER_ADJUST_DISABLED = "speaker" + SEPARATOR
            + "speaker_4.png";

    public static final String SPEAKER_ADJUST_MIN = "speaker" + SEPARATOR
            + "speaker_5.png";

    public static final String SPEAKER_ADJUST_MIN_DISABLED = "speaker"
            + SEPARATOR + "speaker_8.png";

    public static final String DIALPAD = "dialpad" + SEPARATOR
            + "DialPad_1.png";

    public static final String DIALPAD_DISABLED = "dialpad" + SEPARATOR
            + "DialPad_4.png";

    public static final String CALL_END = "call_end" + SEPARATOR
            + "hand_off.png";

    public static final String CALL_RECEIVE = "receivecall" + SEPARATOR
            + "2.png";

    public static final String CALL_RECEIVE_DISABLED = "receivecall"
            + SEPARATOR + "1.png";

    public static final String CONTACT_ADD = "addcontact" + SEPARATOR
            + "addcontact_1.png";

    public static final String CONTACT_ADD_DISABLED = "addcontact" + SEPARATOR
            + "addcontact_4.png";

    public static final String VIDEO_CALL = "video" + SEPARATOR
            + "video_call.png";

    public static final String VIDEO_ON = "video" + SEPARATOR
            + "video_on_1.png";

    public static final String VIDEO_ON_DISABLED = "video" + SEPARATOR
            + "video_on_4.png";

    public static final String VIDEO_OFF = "video" + SEPARATOR
            + "video_off_1.png";

    public static final String VIDEO_OFF_DISABLED = "video" + SEPARATOR
            + "video_off_4.png";

    public static final String CLICK_TO_CALL = "call_toolbar" + SEPARATOR
            + "call_1.png";

    public static final String CLICK_TO_CALL_DISABLED = "call_toolbar"
            + SEPARATOR + "call_2.png";

    public static final String CLICK_TO_CONF = "call_toolbar" + SEPARATOR
            + "conference_1.png";

    public static final String CLICK_TO_CONF_DISABLED = "call_toolbar"
            + SEPARATOR + "conference_2.png";

    public static final String MORE = "comm_dev" + SEPARATOR + "more_1.png";

    public static final String MORE_DISABLED = "comm_dev" + SEPARATOR
            + "more_2.png";

    public static final String PHONE_MODE = "comm_dev" + SEPARATOR
            + "PHONE_3.png";

    public static final String PHONE_MODE_DISABLED = "comm_dev" + SEPARATOR
            + "PHONE_1.png";

    public static final String PC_MODE = "comm_dev" + SEPARATOR + "PC_3.png";

    public static final String PC_MODE_DISABLED = "comm_dev" + SEPARATOR
            + "PC_1.png";

    public static final String ICON_HISTORY = "history" + SEPARATOR
            + "history.png";

    public static final String ICON_HISTORY_MISSED = "history" + SEPARATOR
            + "missed.png";

    public static final String ICON_HISTORY_ANSWERED = "history" + SEPARATOR
            + "answered.png";

    public static final String ICON_HISTORY_DIALED = "history" + SEPARATOR
            + "dialed.png";

    public static final String ICON_WARNING = "errors" + SEPARATOR
            + "warning.png";

    public static final String ICON_CONF_LEADER = "telephony" + SEPARATOR
            + "conf_leader.png";

    public static final String ICON_UCSTATUS_AVAILABLE = "telephony"
            + SEPARATOR + "available.png";

    public static final String ICON_UCSTATUS_BUSY = "telephony" + SEPARATOR
            + "busy.png";

    public static final String ICON_UCSTATUS_DND = "telephony" + SEPARATOR
            + "dnd.png";

    public static final String ICON_UCSTATUS_UNKNOWN = "telephony" + SEPARATOR
            + "unknown.png";

    public static final String ICON_CALLING = "telephony" + SEPARATOR
            + "calling.png";

    public static final String ICON_DIAL_BTN = "dial_btns" + SEPARATOR
            + "DialButton" + SEPARATOR + "Dial.png";

    public static final String ICON_DEL_BTN = "dial_btns" + SEPARATOR
            + "DelButton" + SEPARATOR + "Del.png";

    public static final String AVATOR_DEFAULT = "avator" + SEPARATOR + "1.png";

    public static final String STATUS_AWAY = "status" + SEPARATOR + "Away.png";

    public static final String STATUS_BUSY = "status" + SEPARATOR + "Busy.png";

    public static final String STATUS_HIDE = "status" + SEPARATOR + "hide.png";

    public static final String STATUS_OFFLINE = "status" + SEPARATOR
            + "Offline.png";

    public static final String STATUS_ONLINE = "status" + SEPARATOR
            + "Online.png";

    public static final String NODISTURB = "status" + SEPARATOR
            + "NoDisturb.png";

    public static final String CLOSE_NORMAL = "close" + SEPARATOR
            + "close_normal.png";

    public static final String CLOSE_PRESS = "close" + SEPARATOR
            + "close_press.png";

    /*
     * 二次拨号盘涉及的图片部分
     */
    public static final String SECONDDIALPAD_NUM1_NORMAL = "secondDialPad"
            + SEPARATOR + "num1.png";

    public static final String SECONDDIALPAD_NUM1_ENTER = "secondDialPad"
            + SEPARATOR + "num1Enter.png";

    public static final String SECONDDIALPAD_NUM1_CLICKED = "secondDialPad"
            + SEPARATOR + "num1Clicked.png";

    public static final String SECONDDIALPAD_NUM2_NORMAL = "secondDialPad"
            + SEPARATOR + "num2.png";

    public static final String SECONDDIALPAD_NUM2_ENTER = "secondDialPad"
            + SEPARATOR + "num2Enter.png";

    public static final String SECONDDIALPAD_NUM2_CLICKED = "secondDialPad"
            + SEPARATOR + "num2Clicked.png";

    public static final String SECONDDIALPAD_NUM3_NORMAL = "secondDialPad"
            + SEPARATOR + "num3.png";

    public static final String SECONDDIALPAD_NUM3_ENTER = "secondDialPad"
            + SEPARATOR + "num3Enter.png";

    public static final String SECONDDIALPAD_NUM3_CLICKED = "secondDialPad"
            + SEPARATOR + "num3Clicked.png";

    public static final String SECONDDIALPAD_NUM4_NORMAL = "secondDialPad"
            + SEPARATOR + "num4.png";

    public static final String SECONDDIALPAD_NUM4_ENTER = "secondDialPad"
            + SEPARATOR + "num4Enter.png";

    public static final String SECONDDIALPAD_NUM4_CLICKED = "secondDialPad"
            + SEPARATOR + "num4Clicked.png";

    public static final String SECONDDIALPAD_NUM5_NORMAL = "secondDialPad"
            + SEPARATOR + "num5.png";

    public static final String SECONDDIALPAD_NUM5_ENTER = "secondDialPad"
            + SEPARATOR + "num5Enter.png";

    public static final String SECONDDIALPAD_NUM5_CLICKED = "secondDialPad"
            + SEPARATOR + "num5Clicked.png";

    public static final String SECONDDIALPAD_NUM6_NORMAL = "secondDialPad"
            + SEPARATOR + "num6.png";

    public static final String SECONDDIALPAD_NUM6_ENTER = "secondDialPad"
            + SEPARATOR + "num6Enter.png";

    public static final String SECONDDIALPAD_NUM6_CLICKED = "secondDialPad"
            + SEPARATOR + "num6Clicked.png";

    public static final String SECONDDIALPAD_NUM7_NORMAL = "secondDialPad"
            + SEPARATOR + "num7.png";

    public static final String SECONDDIALPAD_NUM7_ENTER = "secondDialPad"
            + SEPARATOR + "num7Enter.png";

    public static final String SECONDDIALPAD_NUM7_CLICKED = "secondDialPad"
            + SEPARATOR + "num7Clicked.png";

    public static final String SECONDDIALPAD_NUM8_NORMAL = "secondDialPad"
            + SEPARATOR + "num8.png";

    public static final String SECONDDIALPAD_NUM8_ENTER = "secondDialPad"
            + SEPARATOR + "num8Enter.png";

    public static final String SECONDDIALPAD_NUM8_CLICKED = "secondDialPad"
            + SEPARATOR + "num8Clicked.png";

    public static final String SECONDDIALPAD_NUM9_NORMAL = "secondDialPad"
            + SEPARATOR + "num9.png";

    public static final String SECONDDIALPAD_NUM9_ENTER = "secondDialPad"
            + SEPARATOR + "num9Enter.png";

    public static final String SECONDDIALPAD_NUM9_CLICKED = "secondDialPad"
            + SEPARATOR + "num9Clicked.png";

    public static final String SECONDDIALPAD_NUM0_NORMAL = "secondDialPad"
            + SEPARATOR + "num0.png";

    public static final String SECONDDIALPAD_NUM0_ENTER = "secondDialPad"
            + SEPARATOR + "num0Enter.png";

    public static final String SECONDDIALPAD_NUM0_CLICKED = "secondDialPad"
            + SEPARATOR + "num0Clicked.png";

    public static final String SECONDDIALPAD_NUMASTERISK_NORMAL = "secondDialPad"
            + SEPARATOR + "numAsterisk.png";

    public static final String SECONDDIALPAD_NUMASTERISK_ENTER = "secondDialPad"
            + SEPARATOR + "numAsteriskEnter.png";

    public static final String SECONDDIALPAD_NUMASTERISK_CLICKED = "secondDialPad"
            + SEPARATOR + "numAsteriskClicked.png";

    public static final String SECONDDIALPAD_NUMSHARP_NORMAL = "secondDialPad"
            + SEPARATOR + "numSharp.png";

    public static final String SECONDDIALPAD_NUMSHARP_ENTER = "secondDialPad"
            + SEPARATOR + "numSharpEnter.png";

    public static final String SECONDDIALPAD_NUMSHARP_CLICKED = "secondDialPad"
            + SEPARATOR + "numSharpClicked.png";

    // 使用Hashtable保存使用的图片资源
    private static Hashtable<String, Image> htImage = new Hashtable<String, Image>();

    // 加载图片.首先从htImage中获得图片对象，
    // 如果没有，则加载新的图片并放入到htImage
    public static Image loadImage(Display display, String imageName)
    {
        Image image = (Image) htImage.get(imageName);
        // logger.debug("path = " + REAL_PATH + imageName);
        if (null == image)
        {
            ClassLoader currentClassLoader = Thread.currentThread()
                    .getContextClassLoader();
            if (null != currentClassLoader)
            {
                InputStream inputStream = currentClassLoader
                        .getResourceAsStream(REAL_PATH + imageName);
                image = new Image(display, inputStream);
                htImage.put(imageName, image);
                try
                {
                    if (null != inputStream)
                    {
                        inputStream.close();
                        //inputStream为本地变量，使用完成后不必要赋值为null
                        //inputStream = null;
                    }
                }
                catch (IOException exception)
                {
                    logger.error(exception);
                }
            }
            else
            {
                logger.error("null class loader when load image");
            }

        }
        return image;
    }

    // 释放htImage中的所有的图片资源
    public static void dispose()
    {
        Enumeration<Image> e = htImage.elements();
        while (e.hasMoreElements())
        {
            Image image = (Image) e.nextElement();
            if (!image.isDisposed())
                image.dispose();
        }
    }

}
